package com.cenwangkeji.resident.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.TestingBean;
import com.cenwangkeji.resident.model.bean.User;
import com.cenwangkeji.resident.ui.adapter.TestingAdapter;
import com.cenwangkeji.resident.ui.dialog.TestingDialog;
import com.google.gson.Gson;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

//自测记录   血压 血糖 体重 心率
public class TestingActivity extends BaseActivity {

    @BindView(R.id.rv)
    RecyclerView mRv;
    private TestingAdapter mAdapter;

    @Override
    public int setLayoutId() {
        return R.layout.activity_testing;
    }

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        initRecycelrView();
        getData();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @OnClick(R.id.iv)
    public void onViewClicked() {
        TestingDialog dialog = new TestingDialog();
        dialog.setListener(new TestingDialog.Listener() {
            @Override
            public void onAdd() {
                getData();
            }
        });
        dialog.show(getSupportFragmentManager(), "");
    }

    private void initRecycelrView(){
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new TestingAdapter(null);
        mRv.setAdapter(mAdapter);
    }

    private void getData(){
        showLoading();
        Observable.create(new ObservableOnSubscribe<TestingBean>() {
            @Override
            public void subscribe(ObservableEmitter<TestingBean> emitter) throws Exception {
                Map<Object,Object> map = new HashMap<>();
                map.put("mark",3);
                map.put(Constant.token, User.getToken());
                map.put("gId",User.getUserId());
                JSONObject jsonObject = new JSONObject(map);
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.query_tesing);
                MyLogger.i(json);
                Gson gson = new Gson();
                emitter.onNext(gson.fromJson(json, TestingBean.class ));
            }
        }).compose(RxUtils.<TestingBean>io_main()).subscribe(new ObserverOnNext<TestingBean>() {
            @Override
            public void onNext(TestingBean bean) {
                if (bean.getData()!= null&&bean.getData().size()>0) {
                    mAdapter.setNewData(bean.getData());
                }else {
                    View emptyView = getLayoutInflater().inflate(R.layout.view_empty, null);
                    mAdapter.setEmptyView(emptyView);
                    MyToast.show("没有数据");
                }
                dismissLoading();
            }
        });
    }
}
